package com.ebelter.seneo.ui.fragments.main;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoBTManager;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.ui.activitys.SeneoMainActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BMI_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BMR_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BodyAge_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BodyScore_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BodyWater_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Bodyfatpercentage_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Bonemineralcontent_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Musclemass_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Protein_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Size_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Skeletal_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_ToFatWeight_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_VisceralfatLevel_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_WeightControl_Fragment;
import com.ebelter.seneo.ui.fragments.infos.Info_Weight_Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


//填写密码
public class Info_Fragment extends BaseFragment {

    public static final String TAG = "Info_Fragment";

    TabLayout info_tabs;
    ViewPager info_vp;

    private List<Fragment> list;
    private MyAdapter adapter;
    private String[] titles = {"Weight", "Body fat","BMI", "BMR","Protein","Visceral fat",
            "Muscle mass","Bone mass","Body water","Fat-free mass", "Weight control",
            "Skeletal muscles","Body score","Body age","Body type"};


    private Info_Weight_Fragment mInfo_Weight_Fragment;
    private Info_ToFatWeight_Fragment mInfo_ToFatWeight_Fragment;
    private Info_WeightControl_Fragment mInfo_WeightControl_Fragment;
    private Info_BMI_Fragment mInfo_BMI_Fragment;
    private Info_Bodyfatpercentage_Fragment mInfo_Bodyfatpercentage_Fragment;
    private Info_BMR_Fragment mInfo_BMR_Fragment;
    private Info_VisceralfatLevel_Fragment mInfo_VisceralfatLevel_Fragment;
    private Info_Musclemass_Fragment mInfo_Musclemass_Fragment;
    private Info_Bonemineralcontent_Fragment mInfo_Bonemineralcontent_Fragment;
    private Info_Protein_Fragment mInfo_Protein_Fragment;
    private Info_BodyWater_Fragment mInfo_BodyWater_Fragment;
    private Info_Skeletal_Fragment mInfo_Skeletal_Fragment;
    private Info_BodyScore_Fragment mInfo_BodyScore_Fragment;
    private Info_BodyAge_Fragment mInfo_BodyAge_Fragment;
    private Info_Size_Fragment mInfo_Size_Fragment;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.m_info_fly;
    }

    @Override
    protected void initView() {
        //实例化
        info_vp = findViewById(R.id.info_vp);
        info_tabs =  findViewById(R.id.info_tabs);

    }

    @Override
    protected void initData(Bundle arguments) {
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        //页面，数据源
        list = new ArrayList<>();

        mInfo_Weight_Fragment=new Info_Weight_Fragment();
        mInfo_ToFatWeight_Fragment=new Info_ToFatWeight_Fragment();
        mInfo_WeightControl_Fragment=new Info_WeightControl_Fragment();
        mInfo_BMI_Fragment=new Info_BMI_Fragment();
        mInfo_Bodyfatpercentage_Fragment=new Info_Bodyfatpercentage_Fragment();
        mInfo_BMR_Fragment=new Info_BMR_Fragment();
        mInfo_VisceralfatLevel_Fragment=new Info_VisceralfatLevel_Fragment();
        mInfo_Musclemass_Fragment=new Info_Musclemass_Fragment();
        mInfo_Bonemineralcontent_Fragment=new Info_Bonemineralcontent_Fragment();
        mInfo_Protein_Fragment=new Info_Protein_Fragment();
        mInfo_BodyWater_Fragment=new Info_BodyWater_Fragment();
        mInfo_Skeletal_Fragment=new Info_Skeletal_Fragment();
        mInfo_BodyScore_Fragment=new Info_BodyScore_Fragment();
        mInfo_BodyAge_Fragment=new Info_BodyAge_Fragment();

        mInfo_Size_Fragment=new Info_Size_Fragment();

        list.add(mInfo_Weight_Fragment);
        list.add(mInfo_Bodyfatpercentage_Fragment);
        list.add(mInfo_BMI_Fragment);
        list.add(mInfo_BMR_Fragment);

        list.add(mInfo_Protein_Fragment);
        list.add(mInfo_VisceralfatLevel_Fragment);
        list.add(mInfo_Musclemass_Fragment);
        list.add(mInfo_Bonemineralcontent_Fragment);

        list.add(mInfo_BodyWater_Fragment);
        list.add(mInfo_ToFatWeight_Fragment);
        list.add(mInfo_WeightControl_Fragment);

        list.add(mInfo_Skeletal_Fragment);
        list.add(mInfo_BodyScore_Fragment);
        list.add(mInfo_BodyAge_Fragment);
        list.add(mInfo_Size_Fragment);


        //ViewPager的适配器
        adapter = new MyAdapter(getChildFragmentManager());
        info_vp.setAdapter(adapter);
        info_vp.setOffscreenPageLimit(17);
        //绑定
        info_tabs.setupWithViewPager(info_vp);
        info_tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        changeTextColor(info_tabs);


        updateAllCurrentDisplayInfo();
    }

    private void changeTextColor(TabLayout tabLayout){
        try {
            //拿到tabLayout的mTabStrip属性
            Field field = TabLayout.class.getDeclaredField("mTabStrip");
            field.setAccessible(true);
            //拿mTabStrip属性里面的值
            Object mTabStrip = field.get(tabLayout);
            //通过mTabStrip对象来获取class类，不能用field来获取class类，参数不能写Integer.class
            Method method = mTabStrip.getClass().getDeclaredMethod("setSelectedIndicatorColor", int.class);
            method.setAccessible(true);
            method.invoke(mTabStrip, Color.parseColor("#FFFFFFFF"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getCurrentDisplayInfo(String which) {


        if (which.contains(Info_Weight_Fragment.TAG)){
            updateInfoFragment(mInfo_Weight_Fragment,currentUserinfos,currentMeasureRecord);
        }
        if (which.contains(Info_ToFatWeight_Fragment.TAG)){
            updateInfoFragment(mInfo_ToFatWeight_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_WeightControl_Fragment.TAG)){
            updateInfoFragment(mInfo_WeightControl_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_BMI_Fragment.TAG)){
            updateInfoFragment(mInfo_BMI_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_Bodyfatpercentage_Fragment.TAG)){
            updateInfoFragment(mInfo_Bodyfatpercentage_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_BMR_Fragment.TAG)){
            updateInfoFragment(mInfo_BMR_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_VisceralfatLevel_Fragment.TAG)){
            updateInfoFragment(mInfo_VisceralfatLevel_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_Musclemass_Fragment.TAG)){
            updateInfoFragment(mInfo_Musclemass_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_Bonemineralcontent_Fragment.TAG)){
            updateInfoFragment(mInfo_Bonemineralcontent_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_Protein_Fragment.TAG)){
            updateInfoFragment(mInfo_Protein_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_BodyWater_Fragment.TAG)){
            updateInfoFragment(mInfo_BodyWater_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_BodyScore_Fragment.TAG)){
            updateInfoFragment(mInfo_BodyScore_Fragment,currentUserinfos,currentMeasureRecord);
        }
        if (which.contains(Info_BodyAge_Fragment.TAG)){
            updateInfoFragment(mInfo_BodyAge_Fragment,currentUserinfos,currentMeasureRecord);
        }
        if (which.contains(Info_Skeletal_Fragment.TAG)){
            updateInfoFragment(mInfo_Skeletal_Fragment,currentUserinfos,currentMeasureRecord);
        }

        if (which.contains(Info_Size_Fragment.TAG)){
            updateInfoFragment(mInfo_Size_Fragment,currentUserinfos,currentMeasureRecord);
        }

    }


    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }




    private SeneoUserinfos currentUserinfos;
    private SeneoUserCurrentMeasureRecord currentMeasureRecord;

    //更新所有测量的显示信息
    public void updateAllCurrentDisplayInfo(){
        LogUtils.i(TAG,TAG+"---updateAllCurrentDisplayInfo()");
        Activity activity = getActivity();
        if (activity!=null){
            SeneoBTManager seneoBTManager = ((SeneoMainActivity)getActivity()).getSeneoBTManager();
            if (seneoBTManager!=null){
                currentUserinfos = seneoBTManager.getCurrentUserinfos();
                if (currentUserinfos==null){
                    LogUtils.i(TAG,TAG+"---找不到当前用户信息");
                }else {
                    long host_id = currentUserinfos.host_id;
                    long user_id = currentUserinfos.user_id;
                    LogUtils.i2(TAG,"---updateAllCurrentDisplayInfo---host_id="+host_id+"---user_id="+user_id);
                    currentMeasureRecord = SeneoDbUtils.getSeneoUserCurrentMeasureRecord(host_id,user_id);
                    if (currentMeasureRecord==null){
                        LogUtils.i(TAG,TAG+"---找不到当前用户的测量数据");
                    }

                    String which =Info_Weight_Fragment.TAG+Info_ToFatWeight_Fragment.TAG+Info_WeightControl_Fragment.TAG+
                            Info_BMI_Fragment.TAG+Info_Bodyfatpercentage_Fragment.TAG+Info_BMR_Fragment.TAG+Info_VisceralfatLevel_Fragment.TAG+
                            Info_Musclemass_Fragment.TAG+Info_Bonemineralcontent_Fragment.TAG+Info_Protein_Fragment.TAG+
                            Info_BodyWater_Fragment.TAG+Info_Skeletal_Fragment.TAG+Info_Size_Fragment.TAG+ Info_BodyAge_Fragment.TAG+
                            Info_BodyScore_Fragment.TAG;

                    getCurrentDisplayInfo(which);

                }
            }
        }


    }


    private void updateInfoFragment(IInfoUpdateInterface which,SeneoUserinfos s1,SeneoUserCurrentMeasureRecord s2){
        if (which!=null){
            which.UpdateCurrentMesureInfo(s1,s2);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, TAG+"--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd==IUserContant.ICMDS.UPDATE_CURRENT_USER_INFO){
            LogUtils.i(TAG, TAG+"--onEventMainThread更新当前用户信息");
            updateAllCurrentDisplayInfo();
        }
    }



    public interface IInfoUpdateInterface{
        void UpdateCurrentMesureInfo(SeneoUserinfos currentUserinfos,SeneoUserCurrentMeasureRecord currentMeasureRecord);
    }




}
